import Vue from 'vue'
import Cookies from 'js-cookie'
import Setting from '@/Setting'
import VueRouter from 'vue-router'
import routes from '@/router/routes.js'
Vue.use(VueRouter)

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    const token = Cookies.get(Setting.tokenName);

    if (to.path === '/login') {
        if (token) {
            return next('/main/home')
        } else {
            next();
        }
    } else {
        if (token) {
            return next()
        } else {
            return next('/login')
        }
    }

})


export default router